package com.hxy.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.common.JsonResult;
import com.hxy.pojo.Payrecord;
import com.hxy.pojo.model.PayRecordSelectData;
import com.hxy.pojo.vo.PaydetailVo;
import com.hxy.pojo.vo.PayrecordVo;
import com.hxy.service.PayrecordService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

@Controller
@RequestMapping("/payrecord")
public class PayRecordController {
    @Autowired

    private PayrecordService payrecordService;

    /**
     * 显示已经发放的工资
     *
     * @param page
     * @param limit
     * @return
     * @author feiyu
     */
    @RequestMapping("/showAlready")
    @ResponseBody
    public JsonResult show(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                           @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                           @RequestParam(value = "stuffName", required = false, defaultValue = "") String stuffName,
                           @RequestParam(value = "starttime", required = false, defaultValue = "") String startTime,
                           @RequestParam(value = "endtime", required = false, defaultValue = "") String endTime,
                           @RequestParam(value = "paynumber", required = false, defaultValue = "") BigDecimal payNumber
    ) {
        page = page == null ? 1 : page;
        PayRecordSelectData payRecordSelectData = new PayRecordSelectData(page, limit, stuffName, startTime, endTime, payNumber);
        PageInfo<Payrecord> payrecordPageInfo = payrecordService.selectAlreadyPageInfoAll(payRecordSelectData);
        return new JsonResult(0, "msg", payrecordPageInfo);
    }

    /**
     * 未发放的工资
     *
     * @param page
     * @param limit
     * @param stuffName
     * @param branchid
     * @return
     */
    @RequestMapping("/showNot")
    @ResponseBody
    public JsonResult show1(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                            @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                            @RequestParam(value = "stuffname", required = false, defaultValue = "") String stuffName,
                            @RequestParam(value = "branchid", required = false, defaultValue = "") String branchid
    ) {
        page = page == null ? 1 : page;
        PayRecordSelectData payRecordSelectData = new PayRecordSelectData(page, limit, stuffName, branchid);
        PageInfo<Payrecord> payrecordPageInfo = payrecordService.selectNotPageInfoAll(payRecordSelectData);
        return new JsonResult(1, "msg", payrecordPageInfo);
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping("/selectOneByPayRecordVo")
    @ResponseBody
    public JsonResult selectOneByPayRecordVo(Integer id) {
        JsonResult payrecordVo = payrecordService.selectOneByPayRecord(id);
        return payrecordVo;
    }

    /**
     * 删除当前工资记录以及详情
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Integer id) {
        if (id != null && !id.equals("")) {
            //删除记录表
            int i = payrecordService.deletePayrecordByPrimaryKey(id);
            //删除详情表
            int j = payrecordService.deletePaydetailByPrimaryKey(id);
            if ((i + j) > 0) {
                return new JsonResult(0, "success", null);
            } else {
                return new JsonResult(1, "fail", null);
            }

        } else {
            return new JsonResult(1, "fail", null);
        }
    }

    /**
     * //补发工资
     *
     * @param stuffName
     * @param stuffPhone
     * @param sendTime
     * @param receiver
     * @param money
     * @param sender
     * @param status     0未发 1已发 -1补发
     * @return
     */
    @RequestMapping("AddAlready")
    @ResponseBody
    public JsonResult addAlready(
            @RequestParam(value = "stuffName", required = true) String stuffName,
            @RequestParam(value = "stuffPhone", required = true) String stuffPhone,
            @RequestParam(value = "sendTime", required = true) String sendTime,
            @RequestParam(value = "receiver", required = true) String receiver,
            @RequestParam(value = "money", required = true) BigDecimal money,
            @RequestParam(value = "sender", required = true) String sender,
            @RequestParam(value = "status", required = false, defaultValue = "1") String status
    ) {
        JsonResult jsonResult = payrecordService.addAlready(stuffName, stuffPhone, sendTime, receiver, money, sender, status);
        return jsonResult;
    }

    //    更新工资记录以及详情
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "eid", required = false) Integer eid,
            @RequestParam(value = "stuffName", required = false) String stuffName,
            @RequestParam(value = "sendTime", required = false) String sendTime,
            @RequestParam(value = "money", required = false) BigDecimal money,
            @RequestParam(value = "status", required = false, defaultValue = "0") String status
    ) {
        PayrecordVo payrecord = new PayrecordVo();
        payrecord.setId(id);
        payrecord.setEid(eid);
        payrecord.setEname(stuffName);
        payrecord.setPaydate(sendTime);
        payrecord.setPaynumber(money);
        if (status.equals("1")){
            payrecord.setStatus(1);
        }else if(status.equals("0")){
            payrecord.setStatus(0);
        }
        else if (status.equals("-1")){
            payrecord.setStatus(-1);
        }
        JsonResult update = payrecordService.update(payrecord);
        return update;
    }
}








